package org.eoti.rei.ndfcmd;

/**
 * The MIT License
 * <p/>
 * Copyright (c) 2009 Malachi de AElfweald (malachid@gmail.com)
 * <p/>
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * <p/>
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 * <p/>
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
 
import java.io.File;
import java.io.PrintWriter;
import java.io.IOException;
import java.awt.*;

public class PrintCmd
	extends DetailCmd
{
	protected File tmpFile;
	protected PrintWriter out;

	public String[] commands()
	{
		return new String[]{"print"};
	}

	public void detail(int houseNum)
			throws IOException
	{
		if(!Desktop.isDesktopSupported())
		{
			format("'print' is not supported on this platofrm.\n");
			return;
		}

		tmpFile = File.createTempFile("NDFBrowser",".txt");
		tmpFile.deleteOnExit();
		out = new PrintWriter(tmpFile);
		super.detail(houseNum);
		out.flush();
		out.close();
		Desktop.getDesktop().print(tmpFile);
		tmpFile = null;
	}

	public void format(String fmt, Object ... args)
	{
		if(out == null)
			super.format(fmt, args);
		else
			out.format(fmt, args);
	}

	public void displayHelp()
	{
		if(!Desktop.isDesktopSupported())
		{
			format("'print' is not supported on this platofrm.\n");
			return;
		}

		format("print: list available houses and prompt for house to print\n");
		format("print houseNumber: print information for house\n");
	}
}
